package com.hua.controller;


import com.hua.pojo.Food;
import com.hua.pojo.User;
import com.hua.service.UserService;
import com.hua.utils.HandleTokenUtils;
import com.hua.utils.JwtUtils;
import com.hua.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hua
 * @since 2023-12-24
 */
@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired//处理jwt是否有效
    private HandleTokenUtils handleTokenUtils;
    @Autowired//解析jwt
    private JwtUtils jwtUtils;


    /***
     * 登录请求
     * @param requestData
     * @return
     */
    @ApiOperation("判断登录和是否注册")
    @PostMapping("/login")
    public Result login(@RequestBody Map<String, Object> requestData){

        String username = (String) requestData.get("userId");
        String password = (String) requestData.get("password");


        log.info("username:"+username);
        log.info("password:"+password);

        //判断登录和是否注册了
        Map<String, Object> userInfo = userService.login(username, password);
        //登录不成功
        if(userInfo.get("noHasThisAccount")=="true"){
            return  Result.fail(userInfo,"未注册！");
        }else{//已注册
            if(userInfo.get("access")=="true"){//密码验证通过
                return  Result.success(userInfo,"登录成功！");
            }else{//密码验证不通过
                return  Result.fail(userInfo,"密码错误！");
            }
        }
    }


    @ApiOperation("注册请求")
    @PostMapping("/register")
    public Result register(@RequestBody Map<String, Object> requestData){

        String username = (String) requestData.get("userId");
        String password = (String) requestData.get("password");


        log.info("username:"+username);
        log.info("password:"+password);

        Map<String, Object> register = userService.register(username, password);
        //①该账号已存在
        if(register.get("access")=="false"){
            return  Result.fail(register,"该账号已存在！");
        }else{//②存在返回注册失败
            if(register.get("access")=="error"){
                return  Result.fail(register,"系统出错！");
            }else{ //③不存在的进行成功注册的信息返回
                return  Result.success(register,"注册成功！");
            }
        }

    }

    @ApiOperation("用户信息修改")
    @PostMapping("/userInfoModify")
    public Result userInfoModify(@RequestBody Map<String, Object> requestData){

        String token = (String) requestData.get("token");

        String nickname = (String) requestData.get("nickname");
        String avatar_url = (String) requestData.get("avatar_url");


        log.info("token:"+token);
        log.info("nickname:"+nickname);
        log.info("avatar_url:"+avatar_url);


        /**
         * 处理token
         */
        //判断jwt是否还有效
        boolean b = handleTokenUtils.checkJWTIsLost(token, User.class);
        if(!b){//登录失效返回20002状态码
            return Result.loginFailure();
        }


        //登录有效
        //解析token封装到对象
        User user = jwtUtils.parseToken(token, User.class);
        log.info("user——id:"+user.getId());

        //封装:
        User newuser = new User();
        newuser.setId(user.getId());

        newuser.setNickname(nickname);
        newuser.setAvatarUrl(avatar_url);


        Map<String, Object> userInfoModify = userService.userInfoModify(newuser);
        if(userInfoModify==null){
            return Result.fail("系统出错或者请求参数错误，修改失败~~~");
        }else{
            return Result.success("修改成功！");
        }


    }


    @ApiOperation("用户密码修改")
    @PostMapping("/ChangePassword")
    public Result ChangePassword(@RequestBody Map<String, Object> requestData){

        String token = (String) requestData.get("token");
        String oldPassword = (String) requestData.get("oldPassword");
        String newPassword = (String) requestData.get("newPassword");

        /**
         * 处理token
         */
        //判断jwt是否还有效
        boolean b = handleTokenUtils.checkJWTIsLost(token, User.class);
        if(!b){//登录失效
            return Result.loginFailure();
        }

        /**
         * 解析token
         * */
        //解析token到user对象
        User user = jwtUtils.parseToken(token, User.class);


        //检测id和旧密码查询是否相对应
        int check = userService.check(user.getId(), oldPassword);
        if(check==-1){//旧密码不对
            return Result.fail("密码有误！修改出错！");
        }else{//进行密码修改
            int change = userService.change(user.getId(), newPassword);
            if(change==1){//修改成功！
                return Result.success("修改成功！");
            }else{
                return Result.fail("系统出错~请稍后再试~");
            }
        }




    }





}

